package com.demo.io;

import java.awt.Frame;
import java.awt.Image;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.concurrent.TimeUnit;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

/**
 * @ClassName RecieveScreen
 * @Description 该类接收截屏
 */
public class RecieveScreen extends Thread {
    public BorderInit frame;
    public Socket socket;
    public String IP;
    public int Port;
    public long startTime, endTime, s1, e1;

    public static void main(String[] args) {

        new RecieveScreen(new BorderInit(), "10.134.61.119", 8000).start();
    }

    public RecieveScreen(BorderInit frame, String IP, int Port) {
        this.frame = frame;
        this.IP = IP;
        this.Port = Port;
    }

    public void run() {
        while (frame.getFlag()) {
            startTime = System.currentTimeMillis();
            try {
                socket = new Socket(IP, Port);
                DataInputStream ImgInput = new DataInputStream(socket.getInputStream());
                // ZipInputStream imgZip = new ZipInputStream(ImgInput);

                BufferedInputStream bi = new BufferedInputStream(ImgInput);
                // imgZip.getNextEntry();             //到Zip文件流的开始处
//                ImageIO.setUseCache(false);
                s1 = System.currentTimeMillis();
                Image img = ImageIO.read(bi);
                e1 = System.currentTimeMillis();
                System.out.println("Client Read:" + (e1 - s1));
                frame.jlbImg.setIcon(new ImageIcon(img));
                frame.validate();
                TimeUnit.MILLISECONDS.sleep(50);
                bi.close();

            } catch (IOException | InterruptedException e) {
                System.out.println("连接断开");
            } finally {
                try {
                    socket.close();
                } catch (IOException e) {
                }
            }
            endTime = System.currentTimeMillis();
            System.out.println("接收延时: " + (endTime - startTime) + " ms");
        }
    }
}

class BorderInit extends JFrame {
    private static final long serialVersionUID = 1L;
    public JLabel jlbImg;
    private boolean flag;

    public boolean getFlag() {
        return this.flag;
    }

    public BorderInit() {
        this.flag = true;
        this.jlbImg = new JLabel();
        this.setTitle("屏幕分享");
        this.setSize(800, 800);
        this.add(jlbImg);
        this.setLocationRelativeTo(null);
        this.setExtendedState(Frame.MAXIMIZED_BOTH);
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        this.setVisible(true);
        this.validate();

        this.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                flag = false;
                BorderInit.this.dispose();
                System.gc();
            }
        });
    }
}